package com.wrial.kind.doublepointer;
/*
 * @Author  Wrial
 * @Date Created in 12:54 2020/8/7
 * @Description
 *
 * Input: numbers={2, 7, 11, 15}, target=9
   Output: index1=1, index2=2
 */

public class OrderedTwoSum {
    /**
     *
     * @param numbers
     * @param target
     * @return
     * 只有two sum有序才能使用双指针,如果普通twosome 使用HashMap存key为num[i] 然后根据target-key判断map中有没有（需要加上不能重复条件）
     * 双指针法：如果左右指针重叠说明没有符合的值
     */
    public int[] twoSum(int[] numbers, int target) {
        int left = 0;
        int right = numbers.length - 1;

        while (left<right){
            if (target-numbers[left]==numbers[right]){
                return new int[]{left, right};
            }else if (target-numbers[left]>numbers[right]){
                left++;
            }else {
                right++;
            }
        }
        return new int[]{};
    }
}
